﻿using Autofac;
using CSRedis;
using SH.Common;
using SH.Common.DataDictionary;
using SH.Multitenant.Core.Saas;
using SH.Multitenant.Infrastructure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace SH.Multitenant.Infrastructure.Repository
{
    public class DicRepository : IDicRepository
    {
        private readonly ISqlSugarClient _context;
        private readonly CSRedisClient _redis = AppService.AutofacContainer.Resolve<CSRedisClient>();
        public DicRepository()
        {
            _context = DbManger.Db;
        }


        public ISugarQueryable<Dic> All => _context.Queryable<Dic>();
        //数据字典增加
        public async Task<int> DicAddAsync(Dic dic)
        {
            var result = await _context.Insertable<Dic>(dic).ExecuteCommandAsync();
            //if (result <= 0)
            //{
            //    throw new Exception(message: "数据字典修改出错！");                
            //}

            var list = await _context.AsTenant().QueryableWithAttr<Dic>().AS("shmultitenantdb.dic").ToListAsync();
            _redis.Set("dicList", list);

            //else
            //{
            //    list.Add(dic);
            //}

            return result;
        }
        //数据字典删除
        public async Task<int> DicDeleteAsync(Dic dic)
        {
            var result = await _context.Deleteable<Dic>(dic).ExecuteCommandAsync();
            //if (result <= 0)
            //{
            //    throw new Exception(message: "数据字典删除出错！");
            //}
            var list = await _context.AsTenant().QueryableWithAttr<Dic>().AS("shmultitenantdb.dic").ToListAsync();
            _redis.Set("dicList", list);
            //else
            //{
            //    list.Remove(dic);
            //}

            return result;
        }
        //数据字典修改
        public async Task<int> DicUpdAsync(Dic dic)
        {
            var result = await _context.Updateable<Dic>(dic).ExecuteCommandAsync();
            //if (result <= 0)
            //{
            //    throw new Exception(message: "数据字典修改出错！");                
            //}
            var list = await _context.AsTenant().QueryableWithAttr<Dic>().AS("shmultitenantdb.dic").ToListAsync();
            _redis.Set("dicList", list);
            //else
            //{
            //    var entity = list.SingleOrDefault(x => x.Id == dic.Id);
            //    entity = dic;
            //}

            return result;
        }
        //数据字典批量增加
        public async Task<int> DicBatchInsertAsync(List<Dic> list)
        {
            var result = await _context.Insertable(list).ExecuteCommandAsync();
            var redisList = await _context.AsTenant().QueryableWithAttr<Dic>().AS("shmultitenantdb.dic").ToListAsync();
            _redis.Set("dicList", redisList);
            return result;
        }
        //数据字典批量删除
        public async Task<int> BatchDicsDelAsync(List<Dic> list)
        {
            var result = await _context.Deleteable<Dic>(list).ExecuteCommandAsync();
            var redisList = await _context.AsTenant().QueryableWithAttr<Dic>().AS("shmultitenantdb.dic").ToListAsync();
            _redis.Set("dicList", redisList);
            return result;
        }
        //根据唯一标识获取字典实体
        public async Task<Dic> GetDicByIdAsync(int id)
        {
            var dic = await _context.Queryable<Dic>().FirstAsync(x => x.Id == id);
            return dic;
        }
        //根据标识列获取字典列表
        public async Task<List<Dic>> GetDicByIdentificationAsync(string identification)
        {
            return await _context.Queryable<Dic>().Where(x => x.Identification == identification).ToListAsync();
        }

        //获取省市级联列表
        public List<Cascader> GetCascaderList()
        {
            var redisList = _redis.Get<List<Cascader>>("provinceAndCityList");
            if (redisList != null && redisList.Count > 0)
            {
                return redisList;
            }
            var list = _context.Queryable<Dic>().Where(a => a.Identification == "省").Select(a => new Cascader
            {
                value = a.Value.ToString(),
                label = a.Value,
                children = null
            }).ToList();
            foreach (var item in list)
            {
                item.children = _context.Queryable<Dic>().Where(b => b.Identification == "市" && b.Sign == item.label).Select(b => new Cascader
                {
                    value = b.Value.ToString(),
                    label = b.Value,
                    //children = null
                }).ToList();
            }
            _redis.Set("provinceAndCityList", list);
            return list;
        }
    }
    /// <summary>
    /// 级联类
    /// </summary>
    public record Cascader
    {
        public string? value { get; set; }
        public string? label { get; set; }
        public List<Cascader>? children { get; set; }
    }
}
